<?php

class Admin_TintucController extends Zend_Controller_Action{

    public function init()
    {                  
    }        
    
    public function paginatorAct($kq){
        $paginator = Zend_Paginator::factory($kq);
        $paginator->setItemCountPerPage(3);        
        $paginator->setPageRange(3);
        $currentPage = $this->_request->getParam('page',1);
        $paginator->setCurrentPageNumber($currentPage);
        $this->view->data=$paginator;        
    }
    
    public function indexAction(){
       $this->_redirect("/admin/tintuc/main"); 
    }

// -------------------- Tin tức 
        
    public function mainAction(){        
        if($this->_request->isPost()){            
            $param=$this->_request->getParams();                                             
            //$namespace = new Zend_Session_Namespace();    // default namespace            
            //$namespace->setExpirationSeconds(1);          // set session lifetime for second
            $namespace = new Zend_Session_Namespace('adsearch'); // default namespace
            
            if(isset($param[tieude])){
                $namespace->tieude=$param[tieude];                
            }
            if(isset($param[danhmuc1])){
                $namespace->danhmuc1=$param[danhmuc1];                
            }
            if(isset($param[danhmuc2])){
                $namespace->danhmuc2=$param[danhmuc2];                
            }                                                                                                            
        }else{
            Zend_Session::namespaceUnset('adsearch');
        }        
    }    
    
    public function maintintucAction(){        
        $tin=new Admin_Model_Tintuc;
        $item=new Admin_Model_Tintucitem;        
        $cat=new Admin_Model_Tintuccat;        
        $kq=$tin->getAll();                      
            
        $dm1=new Admin_Model_Tintuccat;
        $dm1=$dm1->getAll();
        $arr_dm1['0']='--- Tất cả ---';            
        foreach($dm1 as $item){
            $arr_dm1[$item[id]]=$item[ten];
        }                                                                              
        $form = new Admin_Form_Searchtin;                                        
        $form->getElement('danhmuc1')->setMultiOptions($arr_dm1);
                                     
        $namespace = new Zend_Session_Namespace('adsearch');                
        if(isset($namespace->danhmuc1)){
            $form->getElement('danhmuc1')->setValue($namespace->danhmuc1);
            $arr_search[danhmuc1]=$namespace->danhmuc1;
            if($namespace->danhmuc1!=0){
                $dm2=new Admin_Model_Tintucitem;
                $dm2=$dm2->getArrWhere($namespace->danhmuc1);
                $arr_dm2['0']='--- Tất cả ---';            
                foreach($dm2 as $item){
                    $arr_dm2[$item[id]]=$item[ten];
                }
                $form->getElement('danhmuc2')->setMultiOptions($arr_dm2);
                $form->getElement('danhmuc2')->setValue($namespace->danhmuc2);
                if(isset($namespace->danhmuc2)){
                    $arr_search[danhmuc2]=$namespace->danhmuc2;
                }                                                   
            }                
        }                 
        if(isset($namespace->tieude)){
            $form->getElement('tieude')->setValue($namespace->tieude);
            $arr_search[ten]=$namespace->tieude;    
        }
        if(!empty($arr_search)){            
            $kq=$tin->getaArrWhere($arr_search);
            
        }
              
        $this->view->form=$form;                    
        $this->paginatorAct($kq);              
        $this->_helper->layout->disableLayout();
           
    }
    
    public function hienthitinAction()
    {        
        $tintuc=new Admin_Model_Tintuc;
        $id=$this->_request->getParam("id");        
        $tin=$tintuc->getWhere($id);                             
        if($tin['hienthi']==1)        
            $data=array('hienthi'=>0);            
        else       
            $data=array('hienthi'=>1);                                
        $tintuc->updateTin($data,$id);
        
        $this->_helper->layout->disableLayout();                    
    }
    
    public function deltinAction(){
        $tintuc=new Admin_Model_Tintuc;
        $id=$this->_request->getParam("id");        
                                        
        $tintuc->delTin($id);
        $this->_redirect("/admin/tintuc");
        
        $this->_helper->layout->disableLayout();
    }
    
    public function addtinAction(){
        
    }
    
    public function formaddtinAction(){
        $form = new Admin_Form_Detailstin;   
        $this->view->form=$form;
        $dm1=new Admin_Model_Tintuccat;
        $dm1=$dm1->getAll();                    
        foreach($dm1 as $item){
            $arr_dm1[$item[id]]=$item[ten];
        }
        $form->getElement('danhmuc1')->setMultiOptions($arr_dm1);        
    }
    
// -------------------- Danh mục tin tức cấp 1    
    
    public function getdanhmuc2Action(){        
        $id_dm1 = $this->_request->getParam('id');                
        $dm2=new Admin_Model_Tintucitem;
        $dm2=$dm2->getArrWhere($id_dm1);
        foreach($dm2 as $item){
            $arr_dm2[$item[id]]=$item[ten];
        }
        echo json_encode($arr_dm2);        
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);                                        
    }
    
    public function getdanhmuc1Action(){                                
        $dm1=new Admin_Model_Tintuccat;
        $dm1=$dm1->getAll();
        foreach($dm1 as $item){
            $arr_dm1[$item[id]]=$item[ten];
        }
        echo json_encode($arr_dm1);        
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);                                        
    }
           
    public function maincatAction(){
        $cat=new Admin_Model_Tintuccat;        
        $kq=$cat->getAll();
                
        $this->paginatorAct($kq);
    }
            
    public function mainitemAction(){
        $item=new Admin_Model_Tintucitem;        
        $cat=new Admin_Model_Tintuccat;        
        $kq=$item->getAll();                
        
        $this->paginatorAct($kq);       
    }
            
    public function addcatAction(){
        
    }
    
    public function additemAction(){
        
    }
                
    public function hienthicatAction()
    {        
        $cat=new Admin_Model_Tintuccat;
        $id=$this->_request->getParam("id");        
        $cat_item=$cat->getWhere($id);                             
        if($cat_item['hienthi']==1)        
            $data=array('hienthi'=>0);            
        else        
            $data=array('hienthi'=>1);                                
        $cat->updateCat($data,$id);
        $this->_redirect("/admin/tintuc/mancat/");            
    }
    
    public function hienthiitemAction()
    {        
        $item=new Admin_Model_Tintucitem;
        $id=$this->_request->getParam("id");        
        $cat_item=$item->getWhere($id);                             
        if($cat_item['hienthi']==1)        
            $data=array('hienthi'=>0);            
        else        
            $data=array('hienthi'=>1);                                
        $item->updateItem($data,$id);
        $this->_redirect("/admin/tintuc/manitem/");                  
    }
    
// -------------------- Danh mục tin tức cấp 2    

}

